<?php /**
 * Created by 深圳市丹宏昊天电子商务有限公司.
 * 作 者: sqingwen
 * 日 期: 2017/3/17
 *
 */
?>
<link rel="stylesheet" type="text/css" href="<?= base_url() . 'resource/jeasyui/themes/metro/easyui.css' ?>"/>
<style type="text/css">
    .datagrid-row-over, .datagrid-row-selected {
        background: none;
    }

    .datagrid-row-selected td {
        background-color: #CCE6FF
    }

    .datagrid-row-selected > .datagrid-td-merged, .datagrid-row-selected > td:first-child {
        background: none;
    }

    .datagrid-view {
        　　height: 98px;
    }

</style>

<script type="text/javascript" src="<?=base_url()?>resource/js/layer/layer.js"></script>
<script>
    /**
     * 获取产品分类数据
     */
    function getProductClassData(num){
        var tid='';
        var bid='';
        var item={};
        var html="<option value=''>-请选择-</option>";
        switch (num){
            case 1:
                $('#brand_id').html(html);
                $('#model_id').html(html);
                tid=$('#categoryId').val();
                item={'tid':tid};
                break;
            case 2:
                $('#model_id').html(html);
                tid=$('#categoryId').val();
                bid=$('#brand_id').val();
                item={'tid':tid,'bid':bid};
                break;
            default :
                alert('参数异常');
                return false;
        }
        var load=layer.load(2);
        $.post("<?=blang_url('ajax/ajax/getProductClassAll')?>",item,function(data){
            if(data){
                layer.close(load);
                if(data==101){
                    alert('当前分类下没有任何数据');
                }else {
                    var productData = JSON.parse(data);
                    for (var i = 0; i < productData.length; i++) {
                        html += "<option value='" + productData[i]['sap_id'] + "'>" + productData[i]['name'] + "</option>";
                    }
                    switch (num) {
                        case 1:
                            $('#brand_id').html(html);
                            break;
                        case 2:
                            $('#model_id').html(html);
                            break;
                        default :
                            break;
                    }
                }
            }
        });
    }
</script>

<!--菜单 start-->
<?php $this->load->view('defaults/common/left');?>
<!--菜单 end-->
<div id="content">
    <div class="page-header">
        <div class="container-fluid">
          <h1>础运费管理</h1>
          <?=$this->breadcrumb->generateHTML();?>
        </div>
    </div>
    <div class="container-fluid">
        <div class="panel panel-default">
          <div class="panel-body">
            <div id="p" class="easyui-panel" title="" style="width:100%;padding:10px;">
                <table style="width:99%;">
                    <tr>
                        <td>
                            <!--条件查询 -->
                            <div data-options="region:'north',split:false,border:false,title:'查询条件',collapsed:false,iconCls:'icon-search'">
                                发货地区:<input type="text" name="dtPickUp" id="dtPickUp" class="easyui-validatebox"
                                            maxlength="10" style="height:15px;margin:0 5px 0 5px"/>
                                收货地区:<input type="text" name="dtDelivery" id="dtDelivery"
                                            class="easyui-validatebox"
                                            maxlength="10" style="height:15px;margin:0 5px 0 5px"/>

                                模板名称:<input type="text" name="dtTplname" id="dtTplname"
                                            class="easyui-validatebox"
                                            maxlength="10" style="height:15px;margin:0 5px 0 5px"/>

                                &nbsp;&nbsp;<a href="javascript:void(0)" class="easyui-linkbutton"
                                               data-options="iconCls:'icon-search'" onclick="Search()">查询</a>
                            </div>
                        </td>
                    </tr>

                    <tr>
                        <td>
                            <table id="dataGrid" style="width:99% " class="easyui-table">

                            </table>

                        </td>
                    </tr>
                </table>

            </div>
          </div>
        </div>
    </div>
    
    <!-- 弹出层 begin *************************************************************************************** -->
    <div style="position:relative;left:-10020px">
        <!--closed="true" class="easyui-dialog" title="添加运费模板" data-options="iconCls:'icon-save'"-->
        <!-- 添加运费模板 begin  -->

        <div id="dlg"
             style="width:880px;height:300px;padding:10px;">
            <table style="width:99%;z-index:55">

                <tr>
                    <td>
                        模板名称：
                        <input id="ship_tpl_name" name="ship_tpl_name">


                        发件地区：
                        <select id="selCountry" name="selCountry">
                            <option value="DE">德国</option>
                            <option value="US">美国</option>
                            <option value="UK">英国</option>
                        </select>
                        物流方式：
                        <select id="selType" name="selType">
                <option value="4" class="ooo O_DE">DHL</option>
                            <option value="1" class="ooo O_UK"  style="display:none;">英国邮政Tracked（48H)签名服务</option>
                            <option value="2" class="ooo O_UK"  style="display:none;">英国4PX速递服务</option>
                            <option value="3" class="ooo O_UK"  style="display:none;">德国DHL欧盟区派送</option>





                            <option value="5" class="ooo O_US" style="display:none;">USPS Medium Box</option>
                            <option value="6" class="ooo O_US" style="display:none;">USPS Large Box</option>
                            <option value="7" class="ooo O_US" style="display:none;">USPS padded Envelope</option>
                            <option value="8" class="ooo O_US" style="display:none;">FEDEX本地邮局派送</option>
                <option value="9" class="ooo O_US" style="display:none;">UPS本地标准派送</option>

                        </select>
                        <script type="text/javascript">
                            $('#selCountry').change(function () {
                                $('.ooo').hide();
                                $('#selType').val($('.O_' + $(this).val()).eq(0).attr('value'));
                                $('.O_' + $(this).val()).show();
                                //console.log($(this).val());

                            });
                        </script>
                    </td>
                </tr>
                <tr>
                    <td>
                        <hr>
                    </td>
                </tr>
                <tr>
                    <td>
                        <table style="width:100%;border-collapse:collapse;" cellpadding="0" id="td123">
                            <thead>
                            <tr style="background-color:#ebf3fc;line-height: 32px;">
                                <td align="center" style="width:400px"><strong>收件国</strong></td>
                                <td align="center" style="width:180px"><strong>重量（KG）</strong></td>
                                <td align="center" style="width:100px"><strong>成本价</strong></td>
                                <td align="center" style="width:100px"><strong>销售价</strong></td>
                                <td align="center" style="width:100px"><strong>操作</strong></td>
                            </tr>
                            </thead>
                            <tbody id="idtb">
                            <tr style="height:32px;" class="add1_tr">
                                <td align="center" style="border-bottom: 1px solid #e3e3e3">
                                    <span style="display:none;" class="hidCountryId">0</span>
                                    <span class="showCountry"
                                          style="word-wrap:break-word;width: 387px;float: left;text-align: left;"></span><a
                                            href="javascript:void(0)" class="ckCountry">选择地区</a>
                                </td>
                                <td align="center" style="border-bottom: 1px solid #e3e3e3"><input type="text" value=""
                                                                                                   onkeyup="value = value.replace(/[^\d.]/g, '')"
                                                                                                   class="bgWeight"
                                                                                                   size="5"/> - <input
                                            onkeyup="value = value.replace(/[^\d.]/g, '')"
                                            type="text" value="" size="5" class="endWeight"/></td>
                                <td align="center" style="border-bottom: 1px solid #e3e3e3"><input type="text" value=""
                                                                                                   onkeyup="value = value.replace(/[^\d.]/g, '')"
                                                                                                   class="cost_fee"
                                                                                                   size="5"/></td>
                                <td align="center" style="border-bottom: 1px solid #e3e3e3"><input type="text" value=""
                                                                                                   onkeyup="value = value.replace(/[^\d.]/g, '')"
                                                                                                   class="fee"
                                                                                                   size="5"/></td>
                                <td align="center" style="border-bottom: 1px solid #e3e3e3"><a href="javascript:void(0)"
                                                                                               class="delete"
                                                                                               style="display:none;">删除</a>
                                </td>
                            </tr>
                            </tbody>
                            <tfoot>
                            <tr style="line-height:36px;">
                                <td colspan="5" align="left">
                                    <a href="javascript:void(0)" class="btnAddRow">
                                        + 添加一行
                                    </a>
                                </td>
                            </tr>
                            <script type="text/javascript">


                                $('.endWeight').blur(function () {
                                    var index = $('.endWeight').index($(this));
                                    if ($(this).val() > 0 && $('.bgWeight').eq(index).val() > 0) {
                                        if (parseFloat($(this).val()) <= parseFloat($('.bgWeight').eq(index).val())) {
                                            //alert("您的重量区间是设置不正确，请检查！");
                                            $(this).css("border", "1px solid #db3652");
                                            $('.bgWeight').eq(index).css("border", "1px solid #db3652");
                                            return false;
                                        } else {
                                            $(this).css("border", "1px solid grey");
                                            $('.bgWeight').eq(index).css("border", "1px solid grey");
                                        }
                                    }

                                });

                                $('.bgWeight').blur(function () {
                                    var index = $('.bgWeight').index($(this));
                                    if ($(this).val() > 0 && $('.endWeight').eq(index).val() > 0) {
                                        if (parseFloat($(this).val()) >= parseFloat($('.endWeight').eq(index).val())) {

                                            //alert("您的重量区间是设置不正确，请检查！");
                                            $(this).css("border", "1px solid #db3652");
                                            return false;
                                        } else {
                                            $(this).css("border", "1px solid grey");
                                        }
                                    }
                                });
                                $('.fee,.cost_fee').blur(function () {
                                    if ($(this).val() != '') {
                                        $(this).css('border', '1px solid grey');
                                    }
                                });
                                var tmp = $('#idtb').html();
                                $('.btnAddRow').click(function () {
                                    $('#idtb').find('tr').last().after(tmp);
                                    $(".delete:not(:first)").show();
                                    $(".delete").click(function () {
                                        $(this).parent().parent().remove();
                                    })
                                    $('.endWeight').blur(function () {
                                        var index = $('.endWeight').index($(this));
                                        if ($(this).val() > 0 && $('.bgWeight').eq(index).val() > 0) {
                                            if (parseFloat($(this).val()) <= parseFloat($('.bgWeight').eq(index).val())) {
                                                //alert("您的重量区间是设置不正确，请检查！");
                                                $(this).css("border", "1px solid #db3652");
                                                $('.bgWeight').eq(index).css("border", "1px solid #db3652");
                                                return false;
                                            } else {
                                                $(this).css("border", "1px solid grey");
                                                $('.bgWeight').eq(index).css("border", "1px solid grey");
                                            }
                                        }

                                    });

                                    $('.bgWeight').blur(function () {
                                        var index = $('.bgWeight').index($(this));
                                        if ($(this).val() > 0 && $('.endWeight').eq(index).val() > 0) {
                                            if (parseFloat($(this).val()) >= parseFloat($('.endWeight').eq(index).val())) {

                                                //alert("您的重量区间是设置不正确，请检查！");
                                                $(this).css("border", "1px solid #db3652");
                                                return false;
                                            } else {
                                                $(this).css("border", "1px solid grey");
                                            }
                                        }
                                    });
                                    $('.fee,.cost_fee').blur(function () {
                                        if ($(this).val() != '') {
                                            $(this).css('border', '1px solid grey');
                                        }
                                    });
                                    //这里是关闭【选择地区】按钮之后再打开
                                    $('.ckCountry').click(function () {
                                        unselect();
                                        indexC = $('.ckCountry').index($(this));
                                        selecttt = $('.hidCountryId').eq(indexC).text();
                                        if (selecttt != '0') {
                                            re = new RegExp("_", "g");
                                            strr = selecttt.replace(re, ",")
                                            var strArry = strr.split(',');
                                            for (var i = 0; i < strArry.length; i++) {
                                                var node = $('#ccountry').tree('find', strArry[i]);
                                                $('#ccountry').tree('check', node.target);
                                            }
                                        }
                                        $('#dlgCountry').dialog('open');
                                        $('#hidCountryNo').val($('.ckCountry').index($(this)));

                                    });
                                });


                            </script>

                            <tr style="line-height:36px;">
                                <td colspan="5" align="center">
                                    <a href="javascript:void(0)" class="easyui-linkbutton l-btn l-btn-small"
                                       onclick="submittt()">
                                        添加
                                    </a>
                                    &nbsp;

                                </td>
                            </tr>
                            </tfoot>

                        </table>
                    </td>
                </tr>
            </table>

            <input type="hidden" id="hidCountryNo" name="hidCountryNo" value="0"/>
            <script type="text/javascript">

                //这里是第一次打开按钮的事件
                $('.ckCountry').click(function () {
                    $('#dlgCountry').dialog('open');
                    $('#hidCountryNo').val($('.ckCountry').index($(this)));
                });

            </script>
        </div>
        <!-- 添加运费模板 end  -->
        <script type="text/javascript">
            $('#dlg').hide();
        </script>

        <!-- 修改运费模板 begin  -->
        <div id="dlgMdy" closed="true" class="easyui-dialog" title="修改运费模板" data-options="iconCls:'icon-save'"
             style="width:480px;height:300px;padding:10px;">

            <form id="ffMdy" method="post">
                <div style="margin-bottom:5px">
                    <input class="easyui-textbox" name="mdyName" id="mdyName" style="width:90%"
                           data-options="label:'模板名称:',required:true">
                </div>
                <div style="margin-bottom:5px">
                    <input class="easyui-numberbox" name="mdyBgW" id="mdyBgW" style="width:45%"
                           data-options="label:'起始重量:',required:true,validType:'double',precision:2">
                    -
                    <input class="easyui-numberbox" name="mdyEdW" id="mdyEdW" style="width:45%"
                           data-options="label:'最大重量:',required:true,validType:'double',precision:2">
                </div>
                <div style="margin-bottom:5px">
                    <input class="easyui-numberbox" name="mdyCostFee" id="mdyCostFee" style="width:45%"
                           data-options="label:'预计成本:',required:true,validType:'double',precision:2">
                    -
                    <input class="easyui-numberbox" name="mdyFee" id="mdyFee" style="width:45%"
                           data-options="label:'运费价格:',required:true,validType:'double',precision:2">
                </div>
                <div style="margin-bottom:5px">
                    <select id="mdyPickup" name="mdyPickup" class="easyui-combobox" label="起始地区" style="width:45%">

                        <option value="DE">德国</option>
                        <option value="US">美国</option>
                        <option value="UK">英国</option>
                    </select>
                    -
                    <select id="mdyType" name="mdyType" class="easyui-combobox" label="物流渠道" style="width:45%">
                        <option value="1">英国邮政Tracked（48H)签名服务</option>
                        <option value="2">英国4PX速递服务</option>
                        <option value="3">德国DHL欧盟区派送</option>


                        <option value="4">DHL</option>


                        <option value="5">USPS Medium Box</option>
                        <option value="6">USPS Large Box</option>
                        <option value="7">USPS padded Envelope</option>
                        <option value="8">FEDEX本地邮局派送</option>
               <option value="9">UPS本地标准派送</option>


                    </select>


                    <!--

                    英国：英国邮政Tracked（48H)签名服务，英国4PX速递服务，德国DHL欧盟区派送；
    德国：DHL；
    美国：USPS Medium Box，USPS Large Box，USPS padded Envelope，FEDEX本地邮局派送
                    -->
                    <input type="hidden" id="hidId" name="hidId" value="0">
                    <input type="hidden" id="hidTplCode" name="hidTplCode" value="0">
                    <input type="hidden" id="hidOldTpl" name="hidOldTpl" value="0">
                </div>
                <div style="margin-bottom:5px">
              <input class="easyui-textbox" name="mdyDelivery2" id="mdyDelivery2" style="width:80%;height:60px"
                           data-options="label:'目的地区:',multiline:true,editable:false">
                    <input  name="mdyDelivery" id="mdyDelivery" style="width:80%;height:60px;" type="hidden">
                    <a href="javascript:void(0)" onclick="$('#dlgMdyCountry').dialog('open');">修改地区</a>

                </div>

            </form>
            <div style="text-align:center;padding:5px 0">
                <a href="javascript:void(0)" class="easyui-linkbutton" onclick="submitForm()" style="width:80px">Submit</a>

            </div>

            <script type="text/javascript">
                function submitForm() {
                    $('#ffMdy').form({
                        url: '/sc/users/Shipping/ajaxMdyTpl',
                        onSubmit: function () {
                        },
                        success: function (data) {
                            if (data) {
                                alert("修改成功！");
                                $("#dlgMdy").dialog('close');
                                $('#dataGrid').datagrid('reload');
                            }
                        }
                    });
                    $('#ffMdy').form('submit');
                }
                function clearForm() {
                    $('#ffMdy').form('clear');
                }
            </script>
        </div>
        <!-- 添加运费模板 end  -->


        <!-- 地区模板 begin -->
        <div closed="true" id="dlgCountry" class="easyui-dialog" title="选择地区" data-options="iconCls:'icon-save'"
             style="width:780px;height:380px;padding:5px;">
            <div class="easyui-layout" style="width:750px;height:330px;">
                <div data-options="region:'north'" style="height:45px;padding:5px;">
                    <input class="easyui-searchbox" style="width:50%;float:left;"
                           data-options="prompt:'Please Input ISO or Country',searcher:doSearch">


                    <a href="javascript:void(0)" onclick="getChecked()" style="float: right" class="easyui-linkbutton"
                       data-options="iconCls:'icon-save'">确定</a>
                </div>
                <div data-options="region:'center',title:'选择地区',iconCls:'icon-ok'">
                    <ul id="ccountry" class="easyui-tree"
                        data-options="url:'/sc/users/Shipping/ajaxLoadCountry',method:'get',animate:true,checkbox:true,multiple:true"
                    ></ul>
                </div>
            </div>


        </div>
        <!-- 地区模板 end   -->


        <!-- 修改地区模板 begin -->
        <div closed="true" id="dlgMdyCountry" class="easyui-dialog" title="修改地区" data-options="iconCls:'icon-save'"
             style="width:780px;height:380px;padding:5px;">
            <div class="easyui-layout" style="width:750px;height:330px;">
                <div data-options="region:'north'" style="height:45px;padding:5px;">
                    <input class="easyui-searchbox" style="width:50%;float:left;"
                           data-options="prompt:'Please Input ISO or Country',searcher:doSearch">


                    <a href="javascript:void(0)" onclick="getMdyChecked()" style="float: right" class="easyui-linkbutton"
                       data-options="iconCls:'icon-save'">确定</a>
                </div>
                <div data-options="region:'center',title:'选择地区',iconCls:'icon-ok'">
                    <ul id="ccMdyCountry" class="easyui-tree"
                        data-options="url:'/sc/users/Shipping/ajaxLoadCountry',method:'get',animate:true,checkbox:true,multiple:true"
                    ></ul>
                </div>
            </div>


        </div>
        <!-- 地区模板 end   -->

        <!-- 弹出层 end ***************************************************************************************** -->
    </div>

    <script type="text/javascript">

        $.extend($.fn.datagrid.methods, {
            autoMergeCells: function (jq, fields) {
                return jq.each(function () {
                    var target = $(this);
                    if (!fields) {
                        fields = target.datagrid("getColumnFields");
                    }
                    var rows = target.datagrid("getRows");
                    var i = 0,
                        j = 0,
                        temp = {};
                    for (i; i < rows.length; i++) {
                        var row = rows[i];
                        j = 0;
                        for (j; j < fields.length; j++) {
                            var field = fields[j];
                            var tf = temp[field];
                            if (!tf) {
                                tf = temp[field] = {};
                                tf[row[field]] = [i];
                            } else {
                                var tfv = tf[row[field]];
                                if (tfv) {
                                    tfv.push(i);
                                } else {
                                    tfv = tf[row[field]] = [i];
                                }
                            }
                        }
                    }
                    $.each(temp,
                        function (field, colunm) {
                            $.each(colunm,
                                function () {
                                    var group = this;

                                    if (group.length > 1) {
                                        var before, after, megerIndex = group[0];
                                        for (var i = 0; i < group.length; i++) {
                                            before = group[i];
                                            after = group[i + 1];
                                            if (after && (after - before) == 1) {
                                                continue;
                                            }
                                            var rowspan = before - megerIndex + 1;
                                            if (rowspan > 1) {
                                                target.datagrid('mergeCells', {
                                                    index: megerIndex,
                                                    field: field,
                                                    rowspan: rowspan
                                                });
                                            }
                                            if (after && (after - before) != 1) {
                                                megerIndex = after;
                                            }
                                        }
                                    }
                                });
                        });
                });
            }
        });

        var datagrid;  //定义全局变量
        var editRow = "";//定义当前编辑的行
        datagrid = $("#dataGrid").datagrid({
            title: "基础运费模板设置",
            border: false,
            locale: "zh_CN",
            iconCls: 'icon-save',
            striped: false,
            idField: 'id',
            sortOrder: "desc",
            collapsible: false,
            singleSelect: false,
            url: "/sc/users/Shipping/ajaxLoadBasePrice",
            queryParams: {
                dtPickUp: $('#dtPickUp').val(),
                dtDelivery: $('#dtDelivery').val(),
                dtTplname: $('#dtTplname').val()
            },
            columns: [[
                {
                    field: 'tpl_name',
                    title: '模板名称',
                    width: '18%',
                    algin: 'center',
                    editor: {type: 'text', options: {required: true}}
                },
                {field: 'weight', title: '重量区间（kg）', width: '10%', align: 'center'},
                {field: 'pickup_country', title: '出发地区', width: '8%', align: 'center'},
                {field: 'delivery_country', title: '目的地区', width: '20%', align: 'center'},
                {field: 'delivery_fee', title: '零售价', width: '8%', align: 'center'},
                {field: 'delivery_cost_fee', title: '预计成本', width: '8%', align: 'center'},
                {field: 'shipping_type', title: '物流类型', width: '8%', align: 'center'},
                {
                    field: "_edit",
                    title: '操作',
                    width: '15%',
                    align: 'center',
                    formatter: function (value, rowData, rowIndex) {
                        return "<a href='javascript:void(0)' style='color:#555' onclick='doMdy(" + rowData.id + ")'>修改</a>&nbsp;<a  style='color:#555' href='javascript:void(0)' onclick='doDel(" + rowData.id + ")'>删除</a>";
                    }
                }
            ]],
            toolbar: [{
                id: 'btnAdd',
                text: "添加",
                iconCls: 'icon-add',
                handler: function () {
                    Test("#dlg");

                    //$('#dlg').dialog('open');嗯

                }
            }, {
                id: 'btnEdit',
                text: "批量删除",
                iconCls: 'icon-remove',
                handler: function () {
                    var row = $('#dataGrid').datagrid('getSelections');
                    var ids = '';
                    for (var i = 0; i < row.length; i++) {
                        if (ids != '') {
                            ids += ',';
                        }
                        ids += row[i].id;
                    }
                    $.post('/sc/users/Shipping/ajaxDelTpls', {'ids': ids}, function (result) {
                        if (result > 0) {
                            alert('删除成功！');
                            $('#dataGrid').datagrid('reload');
                        }
                    }, 'text');

                }
            }],
            onLoadSuccess: function (data) {
                //所有列进行合并操作
                //$(this).datagrid("autoMergeCells");
                //指定列进行合并操作
                $(this).datagrid("autoMergeCells", ['groupId', 'tpl_name']);


            },
            pagination: true, //表示在datagrid设置分页
            rownumbers: false,
            singleSelect: false,
        });
        $('#dataGrid').datagrid('getPager').pagination({
            pageSize: 15,
            pageNumber: 1,
            beforePageText: '第', //页数文本框前显示的汉字
            afterPageText: '页    共 {pages} 页',
            displayMsg: '当前显示 {from} - {to} 条记录   共 {total} 条记录',
        });
        //条件查询
        function Search() {
            var queryParameter = $('#dataGrid').datagrid("options").queryParams;
            queryParameter.dtPickUp = $("#dtPickUp").val();
            queryParameter.dtDelivery = $("#dtDelivery").val();
            queryParameter.dtTplname = $("#dtTplname").val();
            $("#dataGrid").datagrid("reload");
        }


        function doSearch(value) {
            $('#ccountry').tree({'url': '/sc/users/Shipping/ajaxLoadCountry?keys=' + value});

        }

        function getChecked() {
            var nodes = $('#ccountry').tree('getChecked');
            //console.log(nodes);
            var s = '';
            var ids = '';
            for (var i = 0; i < nodes.length; i++) {

                if (1==1) {
                    if (s != '') {
                        s += ',';
                    }
                    if (ids != '') {
                        ids += '_';
                    }
                    s += nodes[i].text;
                    ids += nodes[i].id;
                }
            }
            $('.showCountry').eq($('#hidCountryNo').val()).html(s);//这里保存的是选择好的国家名称
            $('.hidCountryId').eq($('#hidCountryNo').val()).text(ids);//这里保存的是选择好的国家的id
            $('#dlgCountry').dialog('close');
        }


        function getMdyChecked() {
            var nodes = $('#ccMdyCountry').tree('getChecked');
            //console.log(nodes);
            var s = '';
            var ids = '';
            for (var i = 0; i < nodes.length; i++) {

                if (1==1) {
                    if (s != '') {
                        s += ',';
                    }

                    if (ids != '') {
                        ids += ',';
                    }
                    s += nodes[i].text;
                    ids += nodes[i].id;
                }
            }
            $("#mdyDelivery").val(ids);
         $("#mdyDelivery2").textbox('setValue', s);
            $('#dlgMdyCountry').dialog('close');
        }


        var dialog;
        function Test(divId) {
            var dialogParent = $(divId).parent();
            var dialogOwn = $(divId).clone();
            dialogOwn.hide();
            dialog = $(divId).dialog({
                autoOpen: true,
                title: '添加运费模板',
                onClose: function () {
                    dialogOwn.appendTo(dialogParent);
                    $(this).dialog("destroy").remove();
                },
                onOpen: function () {
                    $(this).html(dialogOwn.html());
                    $('#ship_tpl_name').combobox({
                        url: '/sc/users/Shipping/ajaxLoadTplname',
                        valueField: 'id',
                        textField: 'text'
                    });

                }
            });


        }


        //保存运费模板
        submittt = function () {

            var tplname = $('#ship_tpl_name').combobox('getText');//运费模板名称
            var tplcode = $('#ship_tpl_name').combobox('getValue');
            var selCountry = $('#selCountry').val();//发货国家
            var selType = $('#selType').val();//物流方式
            var bgW = '';// 起始重量
            var edW = '';//最大重量
            var costFee = '';//成本价
            var fee = '';//价格
            var delCountry = '';//收货国家

            var delivery_bool = 0;
            var end_bool = 0;
            var price_bool = 0;
            $('.bgWeight').each(function () {
                if ($.trim($(this).val()) == '') {
                    end_bool = 1;
                    $(this).css('border', '1px solid #db3652');
                }
                if (bgW != '') {
                    bgW += ',';
                }
                bgW += $.trim($(this).val());
            });

            $('.endWeight').each(function () {
                //这里需要计算一下区间是否符合规范 begin 
                var ewIndex = $('.endWeight').index($(this));
                if (($(this).val() == 0 ) || parseFloat($(this).val()) <= parseFloat($('.bgWeight').eq(ewIndex).val())) {
                    end_bool = 1;
                    $(this).css('border', '1px solid #db3652');
                }

                //这里需要计算一下区间是否符合规范 end
                if (edW != '') {
                    edW += ',';
                }
                edW += $.trim($(this).val());


            });

            $('.hidCountryId').each(function () {
                if ($.trim($(this).text()) == '' || $.trim($(this).text()) == '0') {
                    delivery_bool = 1;
                    $(this).css('border', '1px solid #db3652');
                }
                if (delCountry != '') {
                    delCountry += ',';
                }
                delCountry += $.trim($(this).text());
            });

            $('.cost_fee').each(function () {
                if ($(this).val() == 0 || $.trim($(this).val()) == '') {
                    price_bool = 1;
                    $(this).css('border', '1px solid #db3652');
                }
                if (costFee != '') {
                    costFee += ',';
                }
                costFee += $.trim($(this).val());
            });

            $('.fee').each(function () {
                if ($(this).val() == 0 || $.trim($(this).val()) == '') {
                    price_bool = 1;
                    $(this).css('border', '1px solid #db3652');
                }
                if (fee != '') {
                    fee += ',';
                }
                fee += $.trim($(this).val());
            });
            if (tplname == '') {
                alert("您的模板名称不能为空");
                return false;
            }
            if (delivery_bool == 1) {
                alert("您的发货国家不能为空！");
                return false;
            }
            if (end_bool == 1) {
                alert("您的重量区间设置不正确！");
                return false;
            }
            if (price_bool == 1) {
                alert("您的价格设置不正确！");
                return false;
            }


            $.post('/sc/users/Shipping/ajaxAddShippingTpl', {
                'tplname': tplname,
                'tplcode': tplcode,
                'selCountry': selCountry,
                'selCitys':$('.showCountry').text(),
                'selType': selType,
                'bgW': bgW,
                'edW': edW,
                'costFee': costFee,
                'fee': fee,
                'delCountry': delCountry
            }, function (data) {
                if (data == 1) {
                    $('#dataGrid').datagrid('reload');
                    //$('#dlgCountry').dialog('close');
                    dialog.dialog('close');
                }

            }, 'text');


        }

        unselect = function () {
            var roots = $('#ccountry').tree('getRoots');//返回tree的所有根节点数组
            for (var i = 0; i < roots.length; i++) {
                //console.log(i+'>>:'+roots[i].id);
                var node = $('#ccountry').tree('find', roots[i].id);
                $('#ccountry').tree('uncheck', node.target);
            }

        }

        //删除单行的函数
        doDel = function (dt) {
            $.post('/sc/users/Shipping/ajaxDelById', {'id': dt}, function (result) {
                if (result > 0) {
                    alert('删除成功！');
                    $('#dataGrid').datagrid('reload');
                }
            }, 'text');
        }


        doMdy = function (dt) {
            $('#dlgMdy').dialog('open');
            $.post('/sc/users/Shipping/ajaxLoadTpl', {'id': dt}, function (result) {
                $('#hidId').val(result.id);
                $('#hidTplCode').val(result.tpl_code);
                $('#hidOldTpl').val(result.tpl_name);
                $("#mdyName").textbox('setValue', result.tpl_name);
                $("#mdyBgW").textbox('setValue', result.begin_weight);
                $("#mdyEdW").textbox('setValue', result.end_weight);
                $("#mdyCostFee").textbox('setValue', result.delivery_cost_fee);
                $("#mdyFee").textbox('setValue', result.delivery_fee);
                $("#mdyDelivery").val(result.delivery_country);
          console.log(result.delivery_citys);
            if(result.delivery_citys==''||result.delivery_citys==null)
            {
             $("#mdyDelivery2").textbox('setValue', result.delivery_country);
            }else
            {
             $("#mdyDelivery2").textbox('setValue', result.delivery_citys);
           
            }
           
                $('#mdyPickup').combobox('setValue', result.pickup_country);
                $('#mdyType').combobox('setValue', result.shipping_type);
                var strArry = result.delivery_country.split(',');
                for (var i = 0; i < strArry.length; i++) {
                    var node = $('#ccMdyCountry').tree('find', strArry[i]);
                    $('#ccMdyCountry').tree('check', node.target);
                }
            }, 'json');


        }
    </script>

    <style type="text/css">
        #ccountry > li {
            width: 32%;
            float: left;
        }

    </style>

</div>


